package com.juicygames.shmup.playerbullet;

import android.graphics.Color;
import android.graphics.Rect;

import com.juicygames.framework.action.EntityDestroyedAction;
import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.collider.Collider;
import com.juicygames.framework.collider.TargetCollisionAction;
import com.juicygames.framework.draw.DrawDepth;
import com.juicygames.framework.draw.drawservice.DrawService;
import com.juicygames.framework.gamepart.GameEntity;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.gamepart.GamePart;
import com.juicygames.framework.handler.UpdateHandler;
import com.juicygames.framework.property.space.CollisionBoundingBox;
import com.juicygames.framework.property.space.ColorProperty;
import com.juicygames.framework.property.space.Position;
import com.juicygames.framework.property.space.Velocity;
import com.juicygames.shmup.badguy.BadGuy;
import com.juicygames.shmup.player.DrawHandler;
import com.juicygames.shmup.player.Mover;

public class PlayerBullet extends GameEntity {
	
	private static final int COLOR = Color.BLUE;
	private static final int DRAW_DEPTH = 8;
	
	public void load() {
		
		addPart(new Position(50, 50));
		addPart(new Velocity());
		addPart(new Mover());
		
		DrawHandler drawHandler = addPart(new DrawHandler());
		
		addPart(new CollisionBoundingBox(new Rect(0,0,50,50)));
		Collider collider = addPart(new Collider());
		collider.watchEntity(getWorld().getPart(BadGuy.class));
		addPart(new EnemyHandler());
		
		addPart(new BulletUpdate());
		
		addPart(new ColorProperty(COLOR));
		
		addPart(new DrawDepth(DRAW_DEPTH));
		GamePart world = getWorld();
		world.getPart(DrawService.class).register(drawHandler);
		
	}
	
	public class BulletUpdate extends UpdateHandler {
		
		private int counter = 0;
		
		public void handle(UpdateAction update) {
			if (counter++ > 30) {
				entity.destroy();
			}
		}
	}
	
	public class EnemyHandler extends GameHandler<TargetCollisionAction> {
		
		public Class<TargetCollisionAction> getActionClassHandled() {
			return TargetCollisionAction.class;
		}
		
		public void handle(TargetCollisionAction collision) {
			collision.getTargetEntity().destroy();
		}
	}

}
